package mybatis.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;
import mybatis.entity.Order;
import mybatis.entity.User;
import mybatis.service.GamesService;
import mybatis.service.OrderService;
import mybatis.service.impl.GamesServiceImpl;
import mybatis.service.impl.OrderServiceImpl;

/**
 * Servlet implementation class OrderServlet
 */
@WebServlet("/order/*")
public class OrderServlet extends BaseServlet {
	private OrderService service;
	{
		service = applicationContext.getBean("orderServiceImpl", OrderServiceImpl.class);
	}
	public void submitOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		User user =(User) request.getSession().getAttribute("user");
		String addrId = request.getParameter("addrid");
		System.out.println("addrid==" + addrId);
		Order order =(Order) request.getSession().getAttribute("order");
		System.out.println(order);
		String Order_number = service.submit(order,Integer.parseInt(addrId));
		getOrder(request,response,Order_number);
	}

	public void getOrder(HttpServletRequest request, HttpServletResponse response) {
		String oNum = request.getParameter("oid");
		Order order= service.findByOnumber(oNum);
		System.out.println(order);
		try {
			responseJson(response,order);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void getOrder(HttpServletRequest request, HttpServletResponse response,String orderNumber) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Order order= service.findByOnumber(orderNumber);
		System.out.println(order);
		request.setAttribute("order", order);
		request.getRequestDispatcher("/pages/pay.jsp").forward(request, response);
	}
	public void countOrder(HttpServletRequest request, HttpServletResponse response) {
		User user =(User) request.getSession().getAttribute("user");
		int userId = user.getId();
		List<Order> order = service.findByUid(userId);
		System.out.println(order);
		try {
			responseJson(response,order);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void findOrderByPage(HttpServletRequest request, HttpServletResponse response) {
		User user =(User) request.getSession().getAttribute("user");
		int uid = user.getId();
//		int uid = 10;
		String pageindex = request.getParameter("currentPage");
		int currentPage = Integer.parseInt(pageindex);
		System.out.println(currentPage);
		List<Order> list = service.findPageByUid(uid, currentPage);
		System.out.println(list);
		PageInfo<Order> page = new PageInfo(list);
		try {
			responseJson(response,page);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
